export default class InputHandler {
    constructor(game) {
        this.game=game;
         this.keys = [];
         //为了this指向InputHandler实例，而非window,使用箭头函数
        window.addEventListener('keydown', (e)=> {
            if ((e.key === 'ArrowLeft' ||
                e.key === 'ArrowRight' ||
                e.key === 'ArrowUp' ||
                e.key === 'ArrowDown' ||
                e.key === 'Enter') && this.keys.indexOf(e.key) == -1)
                this.keys.push(e.key)
 
        })

        window.addEventListener('keyup',  (e)=> {
            if (e.key === 'ArrowLeft' ||
                e.key === 'ArrowRight' ||
                e.key === 'ArrowUp' ||
                e.key === 'ArrowDown' ||
                e.key === 'Enter')
                this.keys.splice(this.keys.indexOf(e.key), 1)
            
            

        })
    }
}